package com.cgt.day25;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

public class ReflectDemo {
	@Test
	public void test1() throws ClassNotFoundException {
		Class clazz1 = Class.forName("com.cgt.day25.Student");
		Class clazz2 = Student.class;
		Student student = new Student();
		Class clazz3 = student.getClass();
		System.out.println(clazz1 == clazz2);
		System.out.println(clazz1 == clazz3);
	}

	@Test
	public void test2(){
		Class clazz = Student.class;
		Constructor[] constructors = clazz.getDeclaredConstructors();
		for (Constructor constructor : constructors) {
			System.out.println(constructor);
//			System.out.println(constructor.getModifiers());
//			System.out.println(constructor.getName());
		}
	}

	@Test
	public void test3() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
		Class clazz = Student.class;
		Constructor constructor = clazz.getDeclaredConstructor(Integer.class,String.class,Integer.class,String.class);
		constructor.setAccessible(true);
		Student student = (Student)  constructor.newInstance(1,"zs",23,"男");
		System.out.println(student);
	}

	@Test
	public void test4() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//		Student student = new Student();
		Class clazz = Student.class;
		Constructor constructor = clazz.getConstructor();
		Student student = (Student) constructor.newInstance();
		Method method = clazz.getMethod("setName", String.class);
		method.invoke(student,"zhangsan");
		System.out.println(student);
	}

	@Test
	public void test5(){
		Class clazz = tea.class;
		Constructor[] constructors1 = clazz.getConstructors();
		Constructor[] constructors2 = clazz.getDeclaredConstructors();
//		for (Constructor constructor1 : constructors1) {
//			System.out.println(constructor1);
//		}
//
//		for (Constructor constructor2 : constructors2) {
//			System.out.println(constructor2);
//		}
		Method[] methods = clazz.getMethods();
//		for (Method method : methods) {
//			System.out.println(method);
//		}
		Method[] methods1 = clazz.getDeclaredMethods();
		for (Method method1 : methods1) {
			System.out.println(method1);
		}
	}
}
